﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Api.Common;
using AppManager.BLL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PetaPoco;

namespace Test.Controllers
{
    public class DeviceData
    {
        public int Id { get; set; }
        public string DeviceStr { get; set; }
    }
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestGetDevices()
        {
            Stopwatch sw = Stopwatch.StartNew();
            var messageId = 327;
            //獲取第一條記錄
            var mv = MessageDeviceBLL.GetModel(s => s.MessageId == messageId);
            if (mv != null)
            {
                var count = MessageDeviceBLL.Db.MessageDevices.Count(s => s.MessageId == messageId);
                List<DeviceData> devices = new List<DeviceData>();
                int currentCount = 0;
                int size = 100;
                int id = mv.Id;

                //每隔一千條獲取一次數據
                do
                {
                    devices = MessageDeviceBLL.GetList(s => s.MessageId == messageId &&
                        s.DeviceType == DeviceType.ios &&
                        !string.IsNullOrEmpty(s.DeviceStr) &&
                        s.Id >= mv.Id).OrderBy(s => s.Id).Take(size).Select(s => new DeviceData { Id = s.Id, DeviceStr = s.DeviceStr }).ToList();

                    currentCount += size;
                    id += size;
                    Console.WriteLine(currentCount);

                } while (currentCount <= count);
                sw.Stop();
                Console.WriteLine(sw.Elapsed.Milliseconds);
            }
        }

        [TestMethod]
        public void TestGetDv()
        {
            var db = new Database();
            Stopwatch sw = Stopwatch.StartNew();
            var messageId = 327;
            int devicetype = 0;
            var mv = db.FirstOrDefault<DeviceData>("select Id,DeviceStr from messagedevice where messageid=@0 and devicetype=@1 order by id limit 1", messageId, devicetype);


            if (mv != null)
            {
                var count = db.ExecuteScalar<int>("select count(1) from messagedevice where messageId=@0 and devicetype=@1 and devicestr is not null", messageId, devicetype);
                Console.WriteLine("stratId:{0}  count:{1}", mv.Id, count);

                List<DeviceData> devices = new List<DeviceData>();
                int currentCount = 0;
                int size = 10000;
                int id = mv.Id;

                //每隔一千條獲取一次數據
                do
                {
                    devices = db.Fetch<DeviceData>("select Id,DeviceStr from messagedevice where id>=@0 and messageId=@1 and devicetype=@2 and devicestr is not null limit @3", id, messageId, devicetype, size);

                    currentCount += size;
                    id += size;
                    Console.WriteLine(string.Format("currentCount:{0}    id:{1}", currentCount, id));
                } while (currentCount <= count);
                sw.Stop();
                Console.WriteLine(sw.Elapsed);
            }
        }

        [TestMethod]
        public void TestGetDv2()
        {
            var db = new Database();
            Stopwatch sw = Stopwatch.StartNew();
            var messageId = 327;
            int devicetype = 0;
            var mv = db.FirstOrDefault<DeviceData>("select Id,DeviceStr from messagedevice where messageid=@0 and devicetype=@1 order by id limit 1", messageId, devicetype);


            if (mv != null)
            {
                var count = db.ExecuteScalar<int>("select count(1) from messagedevice where messageId=@0 and devicetype=@1 and devicestr is not null", messageId, devicetype);
                Console.WriteLine("stratId:{0}  count:{1}", mv.Id, count);

                List<DeviceData> devices = new List<DeviceData>();
                int currentCount = 0;

                int id = mv.Id;

                //每隔一千條獲取一次數據

                devices = db.Fetch<DeviceData>("select Id,DeviceStr from messagedevice where id>=@0 and messageId=@1 and devicetype=@2 and devicestr is not null", id, messageId, devicetype);

                Console.WriteLine(string.Format("currentCount:{0}    id:{1}", currentCount, id));
                sw.Stop();
                Console.WriteLine(sw.Elapsed);
            }
        }

        [TestMethod]
        public void TestUpdate()
        {
            var db = new Database();
            Stopwatch sw = Stopwatch.StartNew();


            db.Execute("update messagedevice set failsendcount=failsendcount+1 where messageid=327");

            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
    }
}
